<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="抬头名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入抬头名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票状态" prop="invoiceStatus">
        <el-select v-model="queryParams.invoiceStatus" placeholder="请选择发票状态" clearable>
          <el-option label="待开票" value="1"/>
          <el-option label="已开票" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['parking:memberInvoice:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['parking:memberInvoice:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['parking:memberInvoice:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['parking:memberInvoice:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberInvoiceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" width="80" align="center" prop="id" />-->
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="抬头名称" align="center" prop="title" />
      <el-table-column label="开户银行" align="center" prop="bankName" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="单位地址" align="center" prop="companyAddress" />
      <el-table-column label="税号" align="center" prop="taxNumber" />
      <el-table-column label="电话号码" align="center" prop="telephone" />
      <el-table-column label="开票金额" align="center" prop="amount" />
      <el-table-column label="抬头类型" align="center" prop="invoiceType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.invoiceType === 0" size="small">企业</el-tag>
          <el-tag v-else-if="scope.row.invoiceType === 1" type="warning" size="small">个人</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发票状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="warning" size="small">待开票</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="success" size="small">已开票</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleInvoice(scope.row)"
          >开票</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['parking:memberInvoice:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['parking:memberInvoice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电子发票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="订单号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="发票申请单号" prop="applyId">
          <el-input v-model="form.applyId" placeholder="请输入发票申请单号" />
        </el-form-item>
        <el-form-item label="抬头名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入抬头名称" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="单位地址" prop="companyAddress">
          <el-input v-model="form.companyAddress" placeholder="请输入单位地址" />
        </el-form-item>
        <el-form-item label="税号" prop="taxNumber">
          <el-input v-model="form.taxNumber" placeholder="请输入税号" />
        </el-form-item>
        <el-form-item label="电话号码" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="开票金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入开票金额" />
        </el-form-item>
        <el-form-item label="开票渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入开票渠道" />
        </el-form-item>
        <el-form-item label="开票业务" prop="invoiceBus">
          <el-input v-model="form.invoiceBus" placeholder="请输入开票业务" />
        </el-form-item>
        <el-form-item label="发票地址" prop="invoiceUrl">
          <el-input v-model="form.invoiceUrl" placeholder="请输入发票地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog title="开具发票" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".pdf"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="true"
        drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入PDF格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importInvoiceTemplate">下载发票信息</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUpload()">确 定</el-button>
        <el-button @click="cancelUpload()">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMemberInvoice, getMemberInvoice, delMemberInvoice, addMemberInvoice, updateMemberInvoice, issueInvoice } from "@/api/parking/memberInvoice";
import {getToken} from "@/utils/auth";

export default {
  name: "MemberInvoice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子发票表格数据
      memberInvoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload"
      },
      memberInvoice: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        orderNo: null,
        applyId: null,
        title: null,
        bankName: null,
        bankAccount: null,
        companyAddress: null,
        taxNumber: null,
        telephone: null,
        amount: null,
        invoiceType: null,
        status: null,
        channel: null,
        invoiceBus: null,
        invoiceUrl: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        orderNo: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询电子发票列表 */
    getList() {
      this.loading = true;
      listMemberInvoice(this.queryParams).then(response => {
        this.memberInvoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        orderNo: null,
        applyId: null,
        title: null,
        bankName: null,
        bankAccount: null,
        companyAddress: null,
        taxNumber: null,
        telephone: null,
        amount: null,
        invoiceType: null,
        status: null,
        channel: null,
        invoiceBus: null,
        invoiceUrl: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电子发票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMemberInvoice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电子发票";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMemberInvoice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMemberInvoice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除电子发票编号为"' + ids + '"的数据项？').then(function() {
        return delMemberInvoice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parking/memberInvoice/export', {
        ...this.queryParams
      }, `memberInvoice_${new Date().getTime()}.xlsx`)
    },
    cancelUpload(){
      this.upload.open = false;
      this.memberInvoice = {};
      this.$refs.upload.clearFiles();
    },
    handleInvoice(e){
      this.upload.open = true;
      this.memberInvoice = e;
    },
    importInvoiceTemplate(){
      this.download('parking/memberInvoice/importInvoiceTemplate?id='+this.memberInvoice.id, {
      }, `invoice_data_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.memberInvoice.invoiceUrl = response.fileName;
    },
    submitUpload(){
      let params = {
        id: this.memberInvoice.id,
        orderNo: this.memberInvoice.orderNo,
        invoiceUrl: this.memberInvoice.invoiceUrl
      }
      issueInvoice(params).then(response => {
        this.$modal.msgSuccess("开票成功");
        this.upload.open = false;
        this.$refs.upload.clearFiles();
        this.getList();
      });
    }
  }
};
</script>
